/**
 * 
 */
package com.nari.interfaces.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;

import com.nari.coreframe.service.GenericServiceImpl;
import com.nari.heatshare.dao.IHeatMeterDataGatherDao;
import com.nari.heatshare.dao.IHeatMeterTypeParamDictDao;
import com.nari.heatshare.model.IHeatMeterDataGather;
import com.nari.heatshare.model.IHeatMeterTypeParamDict;
import com.nari.interfaces.dao.IHeatMeterData24GatherDao;
import com.nari.interfaces.dao.springjdbc.IHeatMeterData24GatherJdbcDao;
import com.nari.interfaces.decipher.common.HeatMeterDataPrecisionReviser;
import com.nari.interfaces.decipher.common.HeatMeterDataProtocolDecipher;
import com.nari.interfaces.model.IHeatMeterData24Gather;
import com.nari.interfaces.utils.queryvo.HeatMeterData24RealtimeStatis;
import com.nari.platform.date.CalendarUtils;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResponseMsg;
import com.nari.platform.model.ResultWithPageCond;
import com.nari.platform.utils.CommercialHighPrecisionArith;

/**
 * 
 * @author Xiaopeng
 * @date 2015-1-12
 */
public class HeatMeterData24GatherServiceImpl extends GenericServiceImpl<IHeatMeterData24Gather, String>
		implements IHeatMeterData24GatherService {
	private IHeatMeterData24GatherJdbcDao iHeatMeterData24GatherJdbcDao;
	private IHeatMeterTypeParamDictDao iHeatMeterTypeParamDictDao;
	private IHeatMeterDataGatherDao iHeatMeterDataGatherDao;
	
	/**
	 * 批量新增集抄的热量表数据（规则：每小时上传一次）
	 * @param meterDataList
	 * @param dataFileID 掌机文件或数据文件UID
	 * @param dataType 数据类型（为痕迹文件等备用）
	 * @return 返回成功新增的热量表数据的条目数
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2015-1-15
	 */
	public int saveMeterDataBatch(List<IHeatMeterData24Gather> meterDataList, String dataFileID, 
			Integer dataType) throws Exception {
		//统计批量集抄数据中成功新增的热量表数据的条目数
		int uploadCounter = 0;
		if(meterDataList!=null && meterDataList.size()>0) {
			for(IHeatMeterData24Gather meterData: meterDataList) {
				//关联数据文件引用
				if(dataFileID!=null && !"".equals(dataFileID)) {
					meterData.setDataFileID(dataFileID);
				}
				//修正热量表数据的精度信息
				String typeCode = HeatMeterDataProtocolDecipher.decipherTypeCode(meterData.getWrongInfo());
				IHeatMeterTypeParamDict typeParam = iHeatMeterTypeParamDictDao.queryTypeParamByCode(typeCode);
				if(typeParam != null) {
					meterData = HeatMeterDataPrecisionReviser.reviseMeterDataPrecision(meterData, typeParam);
					//供回水温差
					double temperatureDiff = CommercialHighPrecisionArith.subtract(meterData.getSupplyTemperature(), meterData.getBackTemperature());
					meterData.setTemperatureDiff(temperatureDiff);
				}
				//查询分时热表数据是否已上传
				String meterDataUID = iHeatMeterData24GatherJdbcDao.queryMeterDataUID(meterData.getMeterNo(), meterData.getRecordTime());
				if(meterDataUID!=null && !"".equals(meterDataUID)) {
					meterData.setId(meterDataUID);
					//已上传热表数据，则执行更新
					this.getiGenericDao().updateEntity(meterData, meterDataUID);
				} else {
					this.getiGenericDao().saveEntity(meterData);
					//统计新增的热表数据
					uploadCounter++;
					//强制更新数据到数据库，避免数据进入缓存，便于执行数据去重处理
					this.getiGenericDao().flush(); 
				}
			}
		}
		return uploadCounter;
	}
	
	/**
	 * 热量表分时集抄数据抽取到正式库中
	 * (1)从前一天24小时上传的数据中抽取一条热量表数据，放到正式库中，
	 * 作为系统分摊分析使用（抽取规则是取累计热量为最大值的一条）；
	 * (2)抽取当天日期的（从0点到5点）热量表数据作为当天的抄表记录，
	 * 方便查看使用（抽取规则同上）。
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2015-1-19
	 */
	public void saveExtractMeterDataJobTask() throws Exception {
		//抽取任务的参考日期
		Date today = new Date();
		Date yesterday = CalendarUtils.addDays(today, -1);
		System.out.println("=========数据任务抽取["+CalendarUtils.parseDateTimeToString(today)+"]=========");
		//前一天的数据抽取任务
		List<String> yesterdayMeterNoList = iHeatMeterData24GatherJdbcDao.queryAvailableExtractMeterNo(yesterday);
		this.saveExtractMeterDataBatch(yesterdayMeterNoList, yesterday);
		//当天的数据抽取任务
		List<String> todayMeterNoList = iHeatMeterData24GatherJdbcDao.queryAvailableExtractMeterNo(today);
		this.saveExtractMeterDataBatch(todayMeterNoList, today);
	}
	
	/**
	 * 根据热表表号、抄表日期完成热量表数据的抽取任务
	 * @param meterNoList
	 * @param recordTime
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2015-1-20
	 */
	private void saveExtractMeterDataBatch(List<String> meterNoList, Date recordTime) throws Exception {
		if(meterNoList!=null && meterNoList.size()>0) {
			for(int index=0; index<meterNoList.size(); index++) {
				String meterNo = meterNoList.get(index);
				//抽取热量表数据
				IHeatMeterData24Gather extractMeterData = iHeatMeterData24GatherJdbcDao.queryAvailableExtractMeterData(meterNo, recordTime);
				//生成正式库热量表数据
				IHeatMeterDataGather meterData = new IHeatMeterDataGather();
				BeanUtils.copyProperties(meterData, extractMeterData);
				meterData.setId(null);
				iHeatMeterDataGatherDao.saveOrUpdateMeterData(meterData);
			}
		}
	}
	
	/**
	 * 分页查询热量表分时数据的统计列表
	 * @param meterNo
	 * @param recordTimeStart
	 * @param recordTimeEnd
	 * @param pageCond
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2015-1-21
	 */
	public ResultWithPageCond<HeatMeterData24RealtimeStatis> queryMeterDataStatis(String meterNo, 
			Date recordTimeStart, Date recordTimeEnd, PageCond pageCond) throws Exception {
		return iHeatMeterData24GatherJdbcDao.queryMeterDataStatis(meterNo, recordTimeStart, recordTimeEnd, pageCond);
	}
	
	/**
	 * 根据热量表表号、抄表日期查询热量表分时数据的统计详细信息
	 * @param meterNo
	 * @param recordTime
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2015-1-22
	 */
	public HeatMeterData24RealtimeStatis queryMeterDataStatisDetail(String meterNo, 
			Date recordTime) throws Exception {
		return iHeatMeterData24GatherJdbcDao.queryMeterDataStatisDetail(meterNo, recordTime);
	}
	
	/**
	 * 按统计批次查询热量表分时数据列表
	 * @param meterNo
	 * @param recordTime
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2015-1-22
	 */
	public List<IHeatMeterData24Gather> queryMeterDataList4Statis(String meterNo, 
			Date recordTime) throws Exception {
		IHeatMeterData24GatherDao iHeatMeterData24GatherDao = (IHeatMeterData24GatherDao) this.getiGenericDao();
		return iHeatMeterData24GatherDao.queryMeterDataList4Statis(meterNo, recordTime);
	}
	
	/**
	 * 按统计批次删除热量表分时数据
	 * @param meterNo
	 * @param recordTime
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2015-1-22
	 */
	public ResponseMsg deleteMeterDataStatis(String meterNo, Date recordTime) throws Exception {
		IHeatMeterData24GatherDao iHeatMeterData24GatherDao = (IHeatMeterData24GatherDao) this.getiGenericDao();
		return iHeatMeterData24GatherDao.deleteMeterDataStatis(meterNo, recordTime);
	}

	/**
	 * @return the iHeatMeterData24GatherJdbcDao
	 */
	public IHeatMeterData24GatherJdbcDao getiHeatMeterData24GatherJdbcDao() {
		return iHeatMeterData24GatherJdbcDao;
	}

	/**
	 * @param iHeatMeterData24GatherJdbcDao the iHeatMeterData24GatherJdbcDao to set
	 */
	public void setiHeatMeterData24GatherJdbcDao(
			IHeatMeterData24GatherJdbcDao iHeatMeterData24GatherJdbcDao) {
		this.iHeatMeterData24GatherJdbcDao = iHeatMeterData24GatherJdbcDao;
	}

	/**
	 * @return the iHeatMeterTypeParamDictDao
	 */
	public IHeatMeterTypeParamDictDao getiHeatMeterTypeParamDictDao() {
		return iHeatMeterTypeParamDictDao;
	}

	/**
	 * @param iHeatMeterTypeParamDictDao the iHeatMeterTypeParamDictDao to set
	 */
	public void setiHeatMeterTypeParamDictDao(
			IHeatMeterTypeParamDictDao iHeatMeterTypeParamDictDao) {
		this.iHeatMeterTypeParamDictDao = iHeatMeterTypeParamDictDao;
	}

	/**
	 * @return the iHeatMeterDataGatherDao
	 */
	public IHeatMeterDataGatherDao getiHeatMeterDataGatherDao() {
		return iHeatMeterDataGatherDao;
	}

	/**
	 * @param iHeatMeterDataGatherDao the iHeatMeterDataGatherDao to set
	 */
	public void setiHeatMeterDataGatherDao(
			IHeatMeterDataGatherDao iHeatMeterDataGatherDao) {
		this.iHeatMeterDataGatherDao = iHeatMeterDataGatherDao;
	}
	
}
